<?php
namespace app\community\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\community\Controller;
use think\Db;

class DataCube extends Controller
{
    use \app\community\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = ['add', 'edit', 'delete', 'deleteforever', 'forbid', 'resume', 'recycle', 'recyclebin', 'clear'];

    protected static $isdelete = false;

    public function index(){
        if( $this->request->isPost() ) {
            $from_time1 = explode(' - ',$this->request->post('from_time1/s',''));
            $from_time2 = explode(' - ',$this->request->post('from_time2/s',''));
            if( 2 !== count($from_time1,1) ) {
                return ajax_return_adv_error('请选择对比时间段1');
            }
            if( 2 !== count($from_time2,1) ) {
                return ajax_return_adv_error('请选择对比时间段2');
            }
            $ty_id = getTyId();
            $nature = Db::name('case_nature')->where(['ty_id'=>$ty_id,'isdelete'=>0])->field('id,nature')->select()?:[];
            $model = Db::name('case_godown');
            $list1 = $model->field("count(id) as number,cn_id,FROM_UNIXTIME(from_time,'%Y-%m') as ftime")
                ->where(['from_time'=>['between',[strtotime($from_time1[0]),strtotime($from_time1[1].' 23:59:59')]]])
                ->group('cn_id,ftime')
                ->select()?:[];
            $list2 = $model->field("count(id) as number,cn_id,FROM_UNIXTIME(from_time,'%Y-%m') as ftime")
                ->where(['from_time'=>['between',[strtotime($from_time2[0]),strtotime($from_time2[1].' 23:59:59')]]])
                ->group('cn_id,ftime')
                ->select()?:[];
            $list1Tmp = [];
            foreach ( $list1 as $v ){
                $list1Tmp[$v['ftime']][$v['cn_id']] = $v['number'];
            }
            $list2Tmp = [];
            foreach ( $list2 as $v ){
                $list2Tmp[$v['ftime']][$v['cn_id']] = $v['number'];
            }
            return ajax_return_adv('ok', '', '', false, '', ['option1'=>$this->getEchartsOption($from_time1,$list1Tmp,$nature),'option2'=>$this->getEchartsOption($from_time2,$list2Tmp,$nature)]);
        }
        return $this->view->fetch();
    }


    protected function getEchartsOption($from_time,$list,$nature){
        $option = [
            'color'=> ['#003366','#4cabce', '#006699', '#e5323e'],
            'title'=>[
                'text'=>'案件统计',
                'subtext'=>''
            ],
            'tooltip' =>[
                'trigger'=>'axis',
                'axisPointer' =>[            // 坐标轴指示器，坐标轴触发有效
                    'type' =>'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                ]
            ],
            'legend'=>[
                'data'=>array_column($nature,'nature')
            ],
            'xAxis'=>[
                'type'=>'category',
                'data'=>[]
            ],
            'yAxis'=>[
                'type'=>'value'
            ],
            'series'=>[

            ]
        ];
        foreach ( $nature as $v ) {
            $option['series'][] = [
                'name'=>$v['nature'],
                'data'=>[],
                'type'=>'bar',
                'cn_id'=>$v['id']
            ];
        }
        $noEnd = true;
        $len = 0;
        $time = $from_time[0];
        $endTime = date('Y-m-01',strtotime($from_time[1]));
        $option['title']['subtext'] = $from_time[0]." - ".$from_time[1];
        do{
            $y = substr($time,0,7);
            $option['xAxis']['data'][] = $y;
            if( isset($list[$y]) ) {
                foreach ( $option['series'] as &$v ) {
                    if( isset($list[$y][$v['cn_id']]) ) {
                        $v['data'][] = $list[$y][$v['cn_id']];
                    } else {
                        $v['data'][] = 0;
                    }
                }
            } else {
                foreach ( $option['series'] as &$v ) {
                    $v['data'][] = 0;
                }
            }
            if( $time == $endTime ) {
                $noEnd = false;
            }
            $time = date('Y-m-01',strtotime("$time +1 month"));
            $len += 1;
        }while($noEnd && $len < 100);
        return $option;

    }

}